package designmodel.handler;

import java.text.MessageFormat;
import java.util.Objects;

/**
 * 责任链模式
 *
 * @author scylla
 * @version V1.0
 * @project echo -java-base
 * @title 责任链模式
 * @package designmodel.handler
 * @since 2022 /5/19 5:30 下午
 */
public abstract class AbstractHandler {

    /**
     * The Successor.
     */
    protected AbstractHandler successor;


    /**
     * Set successor.
     *
     * @param successor the successor
     * @return the successor
     */
    public AbstractHandler setSuccessor(AbstractHandler successor) {
        this.successor = successor;
        return this.successor;
    }

    /**
     * Do next.
     *
     * @param request the request
     */
    public void doNext(int request) {
        if (!Objects.isNull(successor)) {
            //传递请求
            successor.handleRequest(request);
        } else {
            finalHandler(request);
        }
    }

    /**
     * Final handler.
     *
     * @param request
     */
    protected void finalHandler(int request) {
        //责任链中任何一个 职责对象 均没有处理，则进行最终处理
        System.out.println(MessageFormat.format("未找到 {0} 对应的职责对象，自动执行最终处理", request));
    }

    /**
     * Handle request.
     *
     * @param request the request
     */
    public abstract void handleRequest(int request);

}
